// *****************************************************
// Aitrox Information Technology
// http://www.proxima-ai.com
// Copyright 2021 Aitrox. All rights reserved.
//
// Filename: thyroid.go
// CreateDate: 2023-05-05
// Author: zjp
// Description: 获取甲状腺病灶数据
// ******************************************************
package export

import (
	"fmt"

	"github.com/jinzhu/gorm"
)

var (
	ETubercleStructCystic              = 0 //囊性
	ETubercleStructMixedCompose        = 1 //囊实混合
	ETubercleStructSolid               = 2 //实性
	ETubercleStructCalcficationMasking = 3 //因钙化掩盖无法评估
)
var (
	ETubercleEchoHigh                = 0 //高回声
	ETubercleEchoEqual               = 1 //等回声
	ETubercleEchoLow                 = 2 //低回声
	ETubercleEchoNone                = 3 //无回声
	ETubercleEchoLower               = 4 //极低回声
	ETubercleEchoCalcficationMasking = 5 //因钙化掩盖无法评估
)
var (
	ETubercleCalcificNone           = 0 //无微钙化
	ETubercleCalcificColloid        = 1 //可疑胶质
	ETubercleCalcificUnclearMeaning = 2 //意义不明确
	ETubercleCalcificTiny           = 3 //微钙化
)
var (
	ETubercleEdgeFitting            = 0 //光整
	ETubercleEdgeIrregular          = 1 //不规则
	ETubercleEdgeVague              = 2 //模糊
	ETubercleEdgeExternalAggression = 3 //甲状腺外侵犯
)
var (
	ETubercleDirectHorizontal = 0 //水平位
	ETubercleDirectVertical   = 1 //垂直位
)

var (
	EUnChecked = 0 //未复核
	EChecked   = 1 //已复核
)

type ThyroidNidusInfo struct {
	*NidusExtInfo
	CheckStatus              uint32  `json:"checkStatus" gorm:"Column:checkStatus"`
	ImageIsDelete            uint16  `json:"imageIsDelete" gorm:"Column:imageIsDelete"`
	LRDiameter               float32 `json:"lrDiameter" gorm:"Column:lrDiameter"`
	FBDiameter               float32 `json:"fbDiameter" gorm:"Column:fbDiameter"`
	Area                     float32 `json:"area" gorm:"Column:area"`
	Lobe                     int     `json:"lobe" gorm:"Column:lobe"`
	Malignant                int     `json:"malignant" gorm:"Column:malignant"`
	MalignantProb            float32 `json:"malignantProb" gorm:"Column:malignantProb"`
	EchogenicFoci            int     `json:"echogenicFoci" gorm:"Column:echogenicFoci"`
	MacroCalcifications      int     `json:"macroCalcifications" gorm:"Column:macroCalcifications"`
	PeripheralCalcifications int     `json:"peripheralCalcifications" gorm:"Column:peripheralCalcifications"`
	Comet                    int     `json:"comet" gorm:"Column:comet"`
	Echogenic                int     `json:"echogenic" gorm:"Column:echogenic"`
	Solid                    int     `json:"solid" gorm:"Column:solid"`
	Circumscribed            int     `json:"circumscribed" gorm:"Column:circumscribed"`
	Tallerthanwide           int     `json:"tallerthanwide" gorm:"Column:tallerthanwide"`
	X1                       int     `json:"x1" gorm:"Column:x1"`
	Y1                       int     `json:"y1" gorm:"Column:y1"`
	X2                       int     `json:"x2" gorm:"Column:x2"`
	Y2                       int     `json:"y2" gorm:"Column:y2"`
}

/**
 * @brief 获取甲状腺病灶数据
 * @param db-数据库操作引擎
 * @param patientIds-患者ID集合
 * @return 病灶数据集合，错误信息
 */
func GetThyroidExportData(db *gorm.DB, patientIds string) ([]*ThyroidNidusInfo, error) {
	return getThyroidExportData(db, patientIds, "t_tubercle_info")
}

/**
 * @brief 获取甲状腺病灶AI原始数据
 * @param db-数据库操作引擎
 * @param patientIds-患者ID集合
 * @return 病灶数据集合，错误信息
 */
func GetThyroidExportDataAi(db *gorm.DB, patientIds string) ([]*ThyroidNidusInfo, error) {
	return getThyroidExportData(db, patientIds, "t_tubercle_info_ai")
}

func getThyroidExportData(db *gorm.DB, patientIds, tbn string) ([]*ThyroidNidusInfo, error) {
	var nidus []*ThyroidNidusInfo

	var query string
	query = "SELECT t1.patient_name, t1.check_id, t1.patient_id, t1.timeStamp, t1.sex, t1.age, t2.imageNTPStamp, t2.checkStatus, t2.isDelete as imageIsDelete, "
	query += "t3.lrDiameter, t3.fbDiameter, t3.area, t3.lobe, t3.malignant, t3.malignantProb, t3.echogenicFoci, t3.macroCalcifications, "
	query += "t3.peripheralCalcifications, t3.comet, t3.echogenic, t3.solid, t3.circumscribed, t3.tallerthanwide, t3.x1, t3.y1, t3.x2, t3.y2"
	query += " FROM t_patient_info t1"
	query += " LEFT JOIN t_image_info t2 on t1.id=t2.patientId"
	query += fmt.Sprintf(" INNER JOIN %s t3 on t2.id=t3.imageId and t1.id=t3.patientId", tbn)
	//导出患者ID
	query += fmt.Sprintf(" WHERE t1.id in (%s)", patientIds)

	res := db.Raw(query).Scan(&nidus)
	if res.Error != nil {
		return nil, res.Error
	}

	return nidus, nil
}
